home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Tool Chest / Files / XTND 1.3.6 / Translator Examples / MacPaint Translator / MacPaintImport.c next >
Encoding:
C/C++ Source or Header  |  1992-03-23  |  4.4 KB  |  144 lines  |  [TEXT/MPS ]

  1. /************************************************************************
  2. *                                                                        *
  3. *    MacPaintImport.c                                                    *
  4. *                                                                        *
  5. *    Translator for reading a MacPaint file with XTND 1.3.                *
  6. *                                                                        *
  7. *    Copyright © 1988 Claris Corporation                                 *
  8. *    All Rights Reserved                                                    *
  9. *                                                                        *
  10. *    Author: Michael Hilton                                                *
  11. *    Date:   15 July, 1988                                                *
  12. *                                                                        *
  13. ************************************************************************/
  14.  
  15. #include <QuickDraw.h>
  16. #include <StandardFile.h>
  17. #include <Memory.h>
  18. #include <Errors.h>
  19. #include <ToolUtils.h>
  20.  
  21. #include ":::XTND Headers:XTNDCIncludes:XTNDPictTranslator.h"
  22.  
  23. /*------------------------- Useful Constants ---------------------------*/
  24.  
  25. #define PNTGHEADERSIZE            512
  26. #define UNCOMPRESSEDDATASIZE    51840
  27.  
  28.  
  29. /*-------------------------Function prototypes--------------------------*/
  30.  
  31.         void    main(PictImportParmBlkPtr);
  32. static    void    ImportPaint(PictImportParmBlkPtr);
  33.  
  34.  
  35. /*----------------------------------------------------------------------*/
  36. /*    main    This is the main routine and the only entry point for the    */
  37. /*            the translator.                                                */
  38. /*----------------------------------------------------------------------*/
  39. void main(ourPtr)
  40. PictImportParmBlkPtr ourPtr;
  41. {
  42.     ImportPaint(ourPtr);    /* Keep things simple at the highest level    */
  43. }
  44.  
  45.  
  46. /*----------------------------------------------------------------------*/
  47. /*    ImportPict    This routine actually performs all of the work of the     */
  48. /*            translator.  It reads and uncompresses the MacPaint file     */
  49. /*            and creates a PICT which is returned to the application.    */
  50. /*----------------------------------------------------------------------*/
  51. static void ImportPaint(ourPtr)
  52. register PictImportParmBlkPtr ourPtr;
  53. {
  54.     register short    i;
  55.     register Ptr    saveSource,saveDest;
  56.     register long    saveDataSize;
  57.  
  58.     Ptr                source, dest;
  59.     long            dataSize;
  60.     GrafPtr            savePort,tempPort;
  61.     BitMap            theBits, saveBits;
  62.     PicHandle        thePicture;
  63.  
  64.     ourPtr->result = noErr;
  65.     
  66.     if (ourPtr->directive != importGetPict)    /* Check for a valid directive */
  67.         return;
  68.     
  69.     /* Determine the size of the compressed data (= size of the file - 512) */
  70.     GetEOF(ourPtr->dataRefNum, &dataSize);    /* Get the size of the file */
  71.     dataSize -= PNTGHEADERSIZE;                /* Subtract the size of the header */
  72.     saveDataSize = dataSize;                /* Save it for later */
  73.  
  74.     saveSource = source = NewPtr(dataSize);    /* Create the source ptr */
  75.     if (ourPtr->result = MemError())
  76.         return;
  77.     
  78.     /* skip over the header information */
  79.     if (ourPtr->result = SetFPos(ourPtr->dataRefNum, fsFromStart, PNTGHEADERSIZE)) {
  80.         DisposPtr(source);
  81.         return;
  82.     }
  83.     
  84.     /* Read in the compressed data */
  85.     if (ourPtr->result = FSRead(ourPtr->dataRefNum, &dataSize, source)) {
  86.         DisposPtr(source);
  87.         return;
  88.     }
  89.     
  90.     if (saveDataSize != dataSize) {            /* If we failed to read in the compressed data */
  91.         DisposPtr(source);
  92.         ourPtr->result = ioErr;
  93.         return;
  94.     }
  95.  
  96.     /* We have successfully read in the compressed data */
  97.     saveDest = dest = NewPtr(UNCOMPRESSEDDATASIZE);    /* Create the dest ptr */
  98.     if (ourPtr->result = MemError()) {
  99.         DisposPtr(source);
  100.         return;
  101.     }
  102.     
  103.     for (i = 0; i < 720; i++)
  104.         UnpackBits(&source, &dest, 72);        /* Unpack the compressed data. */
  105.     
  106.     DisposPtr(saveSource);                    /* Get rid of the source ptr */
  107.     GetPort(&savePort);                        /* save the current port */
  108.     
  109.     /* open a new port (the current port may be a color port) */
  110.     tempPort = (GrafPtr)NewPtr(sizeof(GrafPort));
  111.     if (ourPtr->result = MemError()) {
  112.         DisposPtr(saveDest);
  113.         return;
  114.     }
  115.     OpenPort(tempPort);
  116.     SetPort(tempPort);
  117.  
  118.     /* Set clipping so drawing will work */
  119.     SetRect(&(*tempPort->clipRgn)->rgnBBox, -8000, -8000, 8000, 8000);
  120.     saveBits = tempPort->portBits;
  121.     
  122.     theBits.baseAddr = saveDest;            /* Set up new BitMap */
  123.     theBits.rowBytes = 72;                     /* width of image */
  124.     SetRect(&theBits.bounds, 0, 0, 576, 720);
  125.     SetPortBits(&theBits);                    /* set new port to use the new BitMap */
  126.     
  127.     thePicture = OpenPicture(&theBits.bounds);    /* Open the Picture */
  128.     
  129.     /* Now do a CopyBits to record the bits into the new Picture */
  130.     CopyBits(&theBits, &theBits, &theBits.bounds, &theBits.bounds, srcCopy, 0);
  131.     ClosePicture();                            /* Finished creating picture */
  132.     
  133.     SetPortBits(&saveBits); 
  134.     DisposPtr(saveDest);                    /* Get rid of the dest ptr */
  135.     SetPort(savePort);                        /* set port back to the original port */
  136.  
  137.     ClosePort(tempPort);                    /* get rid of our temporary port */
  138.     DisposPtr((Ptr) tempPort);
  139.     
  140.     /* We're done!! Return the picture. */
  141.     ourPtr->thePicture = thePicture;
  142. }
  143.  
  144.